package com.lhz.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 *                 _ooOoo_
 *                o8888888o
 *                88" . "88
 *                (| -_- |)
 *                O\  =  /O
 *             ____/`---'\____
 *           .'  \\|     |//  `.
 *          /  \\|||  :  |||//  \
 *         /  _||||| -:- |||||-  \
 *         |   | \\\  -  /// |   |
 *         | \_|  ''\---/''  |   |
 *         \  .-\__  `-`  ___/-. /
 *       ___`. .'  /--.--\  `. . __
 *    ."" '<  `.___\_<|>_/___.'  >'"".
 *   | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *   \  \ `-.   \_ __\ /__ _/   .-` /  /
 *====`-.____`-.___\_____/___.-`____.-'======
 *                 `=---='
 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *             佛祖保佑       永无BUG
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhz.dao.DingDanDao;
import com.lhz.dao.DingDanDetailDao;
import com.lhz.dao.mybatis.DingDanManager;
import com.lhz.pojos.Dingdan;
import com.lhz.pojos.Dingdandetail;
@Service
@Transactional
public class DingdanService {

	
	@Autowired
	private DingDanDao jpadao;
	
	@Autowired
	private DingDanManager dindanmybatis;
	
	/**
	 * 订单详细jpadao
	 */
	@Autowired
	private DingDanDetailDao detaijpadao;
	
	
	/**
	 * 显示订单管理界面信息 查询订单Id
	 * @return
	 */
	public Map<String, Object> dindingdanManager(int pageNow,int pageSize,String dingdanId){
		Map<String, Object> map=new HashMap<>();
			if(dingdanId.equals("")) {
				PageHelper.startPage(pageNow, pageSize, true);
				List<Dingdan> searchByDinDanInfo = dindanmybatis.searchByDinDanInfo();
				PageInfo<Dingdan> page=new PageInfo<>(searchByDinDanInfo);
				if(searchByDinDanInfo!=null) {
					map.put("dingdan", searchByDinDanInfo);
					map.put("total", page.getTotal());
					map.put("pageNow", pageNow);
				}else {
					map.put("message", "查询订单信息出问题啦...");
				}
			}
			
			if(!dingdanId.isEmpty()) {
				PageHelper.startPage(pageNow, pageSize, true);
				List<Dingdan> searchByDinDanInfo2 = dindanmybatis.searchByDinDanInfo(dingdanId);
				PageInfo<Dingdan> page=new PageInfo<>(searchByDinDanInfo2);
				if(searchByDinDanInfo2!=null) {
				map.put("dingdan", searchByDinDanInfo2);
				map.put("total", page.getTotal());
				map.put("pageNow", pageNow);
			}else {
				map.put("message", "没有查找这个订单ID呢!");
			}
			}
			
		return map;
		
	}
	
	/**
	 * 根据Id来显示订单详细界面信息
	 * @param dingdanId
	 * @param status
	 * @return
	 */
	public Map<String,Object> dingdandatashow(String dingdan){
		Map<String, Object> map=new HashMap<>();
		if(dingdan==null) {
			map.put("message", "没有找到这个订单的ID哟!");
			return map;
		}
		//id
		Dingdan dindan = jpadao.findOne(dingdan);
		System.err.println("AAAAAAAAAAAAAAAA："+dindan);
		if(dindan!=null) {
			map.put("dingdan", dindan);
		}else {
			map.put("message","没有查到这个订单哟...");
		}
		return map;
	}
	
	
	/**
	 * 退菜
	 * @param dingdanDT_id
	 * @return
	 */
	public Map<String,Object> updatetuicai(Integer dingdanDT_id,String dingdanId){
		Map<String, Object> map=new HashMap<>();
		if(dingdanDT_id==null) {
			map.put("message", "没有找到这个订单的ID哟!");
			return map;
		}
		int a=4;
		Dingdan dindan = jpadao.findOne(dingdanId);
		Dingdandetail dingdandetail = detaijpadao.findOne(dingdanDT_id);
		if(dingdandetail!=null) {
			dingdandetail.setStatus(a);
			dingdandetail.setDingdan(dindan);
			detaijpadao.save(dingdandetail);
			map.put("result", "退菜完成啦!..");
		}else {
			map.put("message", "没有查到菜单相关内容啊!..");
		}
		
		return map;
		
	}
	
	
	
	/**
	 * 修改掉用户完成状态和取消订单
	 * @param dingdanId
	 * @param status
	 * @return
	 */
	public Map<String,Object> updatecompletion(String dingdanId,Integer status){
		System.err.println("AAAAAAAAAAAAAAAAAA:"+status);
		Map<String, Object> map=new HashMap<>();
		if(!dingdanId.isEmpty()||status==null) {
			/**
			 * 修改成订单完成
			 */
			if(status.equals(5)) {
				Integer a=4;
				 Dingdan findByDingdanId = jpadao.findByDingdanId(dingdanId);
				 	if(findByDingdanId!=null) {
				 		  findByDingdanId.setStatus(a);
							jpadao.save(findByDingdanId);
							map.put("result", "交易完成啦!");
							return map;
				 	}else {
				 			map.put("message", "查询订餐出错啦!...");
				 			return map;
				 	}
			}
			/**
			 * 修改取消订单状态
			 */
			if(status.equals(0)) {
				Dingdan findByDingdanId = jpadao.findByDingdanId(dingdanId);
				Integer a=3;
				if(findByDingdanId!=null) {
			 		  findByDingdanId.setStatus(a);
						jpadao.save(findByDingdanId);
						map.put("result", "OK!");
						return map;
			 	}else {
			 			map.put("message", "查询订餐出错啦!...");
			 			return map;
			 	}
			}
			
		}else {
		map.put("message", "没有找到订单Id和状态哟!");
		throw new RuntimeException("没有找到相关id呢！...");
		}
		return map;
	}
	
	
	/**
	 * 下拉框状态查询
	 * @param status
	 * @return
	 */
	public Map<String,Object> searchBySelectInfo(int pageNow,int pageSize,Integer status){
		Map<String,Object> map=new HashMap<>();
		if(status==null) {
			map.put("message", "没有找到这个状态ID哟!..");
			throw new RuntimeSqlException("我在service层没有找到状态Id哟!....");
		}
		/*"0"、未制作订单
		"1"、制作中订单
		"2"、制作完成订单
		"3"、已取消订单
		"4"、交易完成订单
		"5"、上菜完成订单*/
		if(status.equals(1)||status.equals(2)||status.equals(3)||status.equals(4)||status.equals(5)) {
			PageHelper.startPage(pageNow, pageSize, true);
			List<Dingdan> info = dindanmybatis.searchBySelectInfo(status);
			PageInfo<Dingdan> page=new PageInfo<>(info);
			if(info!=null) {
				map.put("dingdan", info);
				map.put("total", page.getTotal());
				map.put("pageNow", pageNow);
			}else {
				map.put("message", "没有查到相关的信息!");
			}
		}else {
			map.put("message", "没有找到指定的状态ID呢!...");
		}
		return map;
		
		
		
	}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
